<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
      <h1>iframe</h1>
      <p>当前数据：
        <span id="iframeSpan" style="color:red;"></span>
      </p>
    <button id="iframeBtn">reduce</button>

    <script>
       var iframeSpan = document.getElementById('iframeSpan');
        var iframeBtn = document.getElementById('iframeBtn');

        const shareWorker = new SharedWorker('./worker   .js', );
        // 使用addEventListener监听消息,需要手动start
        shareWorker.port.addEventListener('message', function(e){
            iframeSpan.innerText = e.data;  
        })
        shareWorker.port.start();
 
        iframeBtn.onclick = function(){
            // 向SharedWorker发送消息
            shareWorker.port.postMessage('reduce');
        }

    </script>
    
</body>
</html>